{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### API实践1：选取三家人脸识别API进行调用\n",
    "目标：\n",
    "* 选取3家人脸识别API，每家API实现3张照片（1张单人、2张多人），比较算出來的年纪、性別及情绪內容并做出这3家API不同之处的**结论**  \n",
    "* 最少需做1家人脸识别API，并实现**3张照片（1张单人、2张多人）的年级、性别及情绪**，\n",
    "\n",
    "\n",
    "* 选用的三家API分别为：\n",
    "Azure、百度、Face++"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Azure\n",
    "* 单人图\n",
    "<smaller>![image](https://cdn.dribbble.com/users/2302612/screenshots/5846605/persona_for_dribbble_copy_2x.jpg)</smaller>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"error\":{\"code\":\"401\",\"message\":\"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource.\"}}'\n"
     ]
    }
   ],
   "source": [
    "import http.client, urllib.request, urllib.parse, urllib.error, base64\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '55c0341ac733482ab48481e34a15ae09',\n",
    "}\n",
    "\n",
    "params = urllib.parse.urlencode({\n",
    "    # Request parameters\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    'returnFaceAttributes': '{string}',\n",
    "})\n",
    "\n",
    "try:\n",
    "    conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')\n",
    "    conn.request(\"POST\", \"/face/v1.0/detect?%s\" % params, \"{body}\", headers)\n",
    "    response = conn.getresponse()\n",
    "    data = response.read()\n",
    "    print(data)\n",
    "    conn.close()\n",
    "except Exception as e:\n",
    "    print(\"[Errno {0}] {1}\".format(e.errno, e.strerror))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '971f5759-87b7-4a2f-8cda-b3a9256ffd2d',\n",
       "  'faceRectangle': {'top': 134, 'left': 137, 'width': 105, 'height': 105},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 22.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 1.0,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}}]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "KEY = '55c0341ac733482ab48481e34a15ae09'\n",
    "BASE_URL = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect' \n",
    "KEY = '55c0341ac733482ab48481e34a15ae09'\n",
    "\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),\n",
    "}\n",
    "\n",
    "img_url = 'https://cdn.dribbble.com/users/2302612/screenshots/5846605/persona_for_dribbble_copy_2x.jpg'\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), \n",
    "}\n",
    "\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)\n",
    "r.status_code\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 多人图1\n",
    "<smaller>![image](https://azurecomcdn.azureedge.net/cvt-07cd79bcf94c7abee51d2d07f52c7f42b4595e8b2fca2825cdbf8d1bd6912e4b/images/shared/cognitive-services-demos/face-detection/detection-5-thumbnail.jpg)</smaller>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '67b0e3ff-83d7-42ba-8825-c1cdf9520d4b',\n",
       "  'faceRectangle': {'top': 50, 'left': 130, 'width': 64, 'height': 64},\n",
       "  'faceAttributes': {'gender': 'male', 'age': 25.0, 'glasses': 'NoGlasses'}},\n",
       " {'faceId': '0475555b-8a3d-48e6-bb21-28f968d4ea7d',\n",
       "  'faceRectangle': {'top': 56, 'left': 16, 'width': 57, 'height': 57},\n",
       "  'faceAttributes': {'gender': 'female', 'age': 28.0, 'glasses': 'NoGlasses'}},\n",
       " {'faceId': 'dfaa640c-3dba-4e73-bea1-96ab222060b6',\n",
       "  'faceRectangle': {'top': 104, 'left': 90, 'width': 41, 'height': 41},\n",
       "  'faceAttributes': {'gender': 'female', 'age': 1.0, 'glasses': 'NoGlasses'}}]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "KEY = '55c0341ac733482ab48481e34a15ae09'\n",
    "BASE_URL = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect' \n",
    "KEY = '55c0341ac733482ab48481e34a15ae09'\n",
    "\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),\n",
    "}\n",
    "\n",
    "img_url = 'https://azurecomcdn.azureedge.net/cvt-07cd79bcf94c7abee51d2d07f52c7f42b4595e8b2fca2825cdbf8d1bd6912e4b/images/shared/cognitive-services-demos/face-detection/detection-5-thumbnail.jpg'\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses'), \n",
    "}\n",
    "\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)\n",
    "\n",
    "r.status_code\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 多人图2\n",
    "![image](http://newmedia.nfu.edu.cn/wcy/wp-content/uploads/2018/04/post_20180424__NFU_DoraHacks_imoji%E5%9B%A2%E9%98%9F.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '22f6ac94-855e-4340-9397-a37fed3c6f7f',\n",
       "  'faceRectangle': {'top': 118, 'left': 144, 'width': 88, 'height': 88},\n",
       "  'faceAttributes': {'gender': 'male', 'age': 18.0, 'glasses': 'NoGlasses'}},\n",
       " {'faceId': '684a144d-6166-4cc2-ae83-84ca38798f46',\n",
       "  'faceRectangle': {'top': 117, 'left': 376, 'width': 64, 'height': 64},\n",
       "  'faceAttributes': {'gender': 'female', 'age': 20.0, 'glasses': 'NoGlasses'}},\n",
       " {'faceId': 'e6580ffd-f734-43c0-87d9-bbd15dfaf49c',\n",
       "  'faceRectangle': {'top': 41, 'left': 676, 'width': 52, 'height': 52},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 23.0,\n",
       "   'glasses': 'ReadingGlasses'}},\n",
       " {'faceId': 'fd7752ec-916b-4d35-a6be-92255069537a',\n",
       "  'faceRectangle': {'top': 69, 'left': 445, 'width': 52, 'height': 52},\n",
       "  'faceAttributes': {'gender': 'female', 'age': 21.0, 'glasses': 'NoGlasses'}},\n",
       " {'faceId': '60134da3-d555-45f6-8db4-569ff31e7567',\n",
       "  'faceRectangle': {'top': 95, 'left': 238, 'width': 51, 'height': 51},\n",
       "  'faceAttributes': {'gender': 'female',\n",
       "   'age': 17.0,\n",
       "   'glasses': 'ReadingGlasses'}},\n",
       " {'faceId': 'bff3412f-5741-4a61-8a9e-f0ffa09ec337',\n",
       "  'faceRectangle': {'top': 94, 'left': 540, 'width': 48, 'height': 48},\n",
       "  'faceAttributes': {'gender': 'female', 'age': 19.0, 'glasses': 'NoGlasses'}}]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "KEY = '55c0341ac733482ab48481e34a15ae09'\n",
    "BASE_URL = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect' \n",
    "KEY = '55c0341ac733482ab48481e34a15ae09'\n",
    "\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),\n",
    "}\n",
    "\n",
    "img_url = 'http://newmedia.nfu.edu.cn/wcy/wp-content/uploads/2018/04/post_20180424__NFU_DoraHacks_imoji%E5%9B%A2%E9%98%9F.jpg'\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses'), \n",
    "}\n",
    "\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)\n",
    "\n",
    "r.status_code\n",
    "r.json()    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Face++\n",
    "* 单人图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"request_id\":\"1586704471,19e3744f-eaee-4a21-9948-624c4ba4a2c4\",\"time_used\":631,\"faces\":[{\"face_token\":\"7115dbce4d4be50140881dd3607753a1\",\"face_rectangle\":{\"top\":251,\"left\":167,\"width\":169,\"height\":169},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":21},\"smile\":{\"value\":0.007,\"threshold\":50.000},\"emotion\":{\"anger\":1.354,\"disgust\":0.028,\"fear\":0.028,\"happiness\":0.046,\"neutral\":58.084,\"sadness\":0.225,\"surprise\":40.236}}}],\"image_id\":\"xDGMLGg3GCf5Ioiaf/OZJQ==\",\"face_num\":1}\\n'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'request_id': '1586704471,19e3744f-eaee-4a21-9948-624c4ba4a2c4',\n",
       " 'time_used': 631,\n",
       " 'faces': [{'face_token': '7115dbce4d4be50140881dd3607753a1',\n",
       "   'face_rectangle': {'top': 251, 'left': 167, 'width': 169, 'height': 169},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 21},\n",
       "    'smile': {'value': 0.007, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 1.354,\n",
       "     'disgust': 0.028,\n",
       "     'fear': 0.028,\n",
       "     'happiness': 0.046,\n",
       "     'neutral': 58.084,\n",
       "     'sadness': 0.225,\n",
       "     'surprise': 40.236}}}],\n",
       " 'image_id': 'xDGMLGg3GCf5Ioiaf/OZJQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "api_secret = \"pqLlByl2B4NhSGB52OfdeaiGyuDgh4xu\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'D2we8r0_5B9JgDjTXbdl6eiXRFCTHEmg'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://www.faceplusplus.com.cn/scripts/demoScript/images/demo-pic8.jpg'\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "print(r.content)\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 多人图1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"request_id\":\"1586704540,7f0b1231-2da0-4d51-a6d0-fe1ade84efcb\",\"time_used\":4016,\"faces\":[{\"face_token\":\"2914966c890fa70969e1a66e71a677a0\",\"face_rectangle\":{\"top\":56,\"left\":134,\"width\":61,\"height\":61},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":29},\"smile\":{\"value\":100.000,\"threshold\":50.000},\"emotion\":{\"anger\":0.000,\"disgust\":0.003,\"fear\":0.000,\"happiness\":99.943,\"neutral\":0.000,\"sadness\":0.000,\"surprise\":0.053}}},{\"face_token\":\"ee67a95a617c443b5711bd67c31d271d\",\"face_rectangle\":{\"top\":60,\"left\":13,\"width\":56,\"height\":56},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":31},\"smile\":{\"value\":100.000,\"threshold\":50.000},\"emotion\":{\"anger\":0.000,\"disgust\":0.001,\"fear\":0.000,\"happiness\":99.999,\"neutral\":0.000,\"sadness\":0.000,\"surprise\":0.000}}},{\"face_token\":\"f611b70e06372e55148a0cca087fa9d4\",\"face_rectangle\":{\"top\":107,\"left\":92,\"width\":40,\"height\":40},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":7},\"smile\":{\"value\":0.010,\"threshold\":50.000},\"emotion\":{\"anger\":0.085,\"disgust\":0.008,\"fear\":69.255,\"happiness\":0.006,\"neutral\":0.006,\"sadness\":0.006,\"surprise\":30.634}}}],\"image_id\":\"hFDGIm0DTSB+dlrmsp+Vtw==\",\"face_num\":3}\\n'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'request_id': '1586704540,7f0b1231-2da0-4d51-a6d0-fe1ade84efcb',\n",
       " 'time_used': 4016,\n",
       " 'faces': [{'face_token': '2914966c890fa70969e1a66e71a677a0',\n",
       "   'face_rectangle': {'top': 56, 'left': 134, 'width': 61, 'height': 61},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 29},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.003,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 99.943,\n",
       "     'neutral': 0.0,\n",
       "     'sadness': 0.0,\n",
       "     'surprise': 0.053}}},\n",
       "  {'face_token': 'ee67a95a617c443b5711bd67c31d271d',\n",
       "   'face_rectangle': {'top': 60, 'left': 13, 'width': 56, 'height': 56},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 31},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.001,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 99.999,\n",
       "     'neutral': 0.0,\n",
       "     'sadness': 0.0,\n",
       "     'surprise': 0.0}}},\n",
       "  {'face_token': 'f611b70e06372e55148a0cca087fa9d4',\n",
       "   'face_rectangle': {'top': 107, 'left': 92, 'width': 40, 'height': 40},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 7},\n",
       "    'smile': {'value': 0.01, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.085,\n",
       "     'disgust': 0.008,\n",
       "     'fear': 69.255,\n",
       "     'happiness': 0.006,\n",
       "     'neutral': 0.006,\n",
       "     'sadness': 0.006,\n",
       "     'surprise': 30.634}}}],\n",
       " 'image_id': 'hFDGIm0DTSB+dlrmsp+Vtw==',\n",
       " 'face_num': 3}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "api_secret = \"pqLlByl2B4NhSGB52OfdeaiGyuDgh4xu\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'D2we8r0_5B9JgDjTXbdl6eiXRFCTHEmg'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://azurecomcdn.azureedge.net/cvt-07cd79bcf94c7abee51d2d07f52c7f42b4595e8b2fca2825cdbf8d1bd6912e4b/images/shared/cognitive-services-demos/face-detection/detection-5-thumbnail.jpg'\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "print(r.content)\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 多人图2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"request_id\":\"1586704513,1b243594-fd17-4e02-a31b-2332eb298227\",\"time_used\":1309,\"faces\":[{\"face_token\":\"10b33b0810b783df8bef29d64cd5bdec\",\"face_rectangle\":{\"top\":118,\"left\":-13,\"width\":170,\"height\":170},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":60},\"smile\":{\"value\":100.000,\"threshold\":50.000},\"emotion\":{\"anger\":0.000,\"disgust\":0.000,\"fear\":0.000,\"happiness\":100.000,\"neutral\":0.000,\"sadness\":0.000,\"surprise\":0.000}}},{\"face_token\":\"a9a932bf141eff181180d1f5d52990b8\",\"face_rectangle\":{\"top\":128,\"left\":144,\"width\":161,\"height\":161},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":60},\"smile\":{\"value\":100.000,\"threshold\":50.000},\"emotion\":{\"anger\":0.000,\"disgust\":0.000,\"fear\":0.000,\"happiness\":100.000,\"neutral\":0.000,\"sadness\":0.000,\"surprise\":0.000}}}],\"image_id\":\"w0DCeZw7EJY4ygfO7/qhUA==\",\"face_num\":2}\\n'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'request_id': '1586704513,1b243594-fd17-4e02-a31b-2332eb298227',\n",
       " 'time_used': 1309,\n",
       " 'faces': [{'face_token': '10b33b0810b783df8bef29d64cd5bdec',\n",
       "   'face_rectangle': {'top': 118, 'left': -13, 'width': 170, 'height': 170},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 60},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 100.0,\n",
       "     'neutral': 0.0,\n",
       "     'sadness': 0.0,\n",
       "     'surprise': 0.0}}},\n",
       "  {'face_token': 'a9a932bf141eff181180d1f5d52990b8',\n",
       "   'face_rectangle': {'top': 128, 'left': 144, 'width': 161, 'height': 161},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 60},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 100.0,\n",
       "     'neutral': 0.0,\n",
       "     'sadness': 0.0,\n",
       "     'surprise': 0.0}}}],\n",
       " 'image_id': 'w0DCeZw7EJY4ygfO7/qhUA==',\n",
       " 'face_num': 2}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "api_secret = \"pqLlByl2B4NhSGB52OfdeaiGyuDgh4xu\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = 'D2we8r0_5B9JgDjTXbdl6eiXRFCTHEmg'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://www.faceplusplus.com.cn/scripts/demoScript/images/demo-pic10.jpg'\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "\n",
    "r.status_code\n",
    "print(r.content)\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 百度\n",
    "* 图1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"error_code\":0,\"error_msg\":\"SUCCESS\",\"log_id\":7515896525101,\"timestamp\":1586715323,\"cached\":0,\"result\":{\"face_num\":1,\"face_list\":[{\"face_token\":\"06841e7fb1a3ca89e4e1e9725e493846\",\"location\":{\"left\":447.42,\"top\":430.05,\"width\":263,\"height\":222,\"rotation\":0},\"face_probability\":1,\"angle\":{\"yaw\":-23.43,\"pitch\":21.28,\"roll\":6.66},\"age\":6,\"gender\":{\"type\":\"male\",\"probability\":0.95},\"expression\":{\"type\":\"smile\",\"probability\":1},\"emotion\":{\"type\":\"happy\",\"probability\":0.99}}]}}'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 7515896525101,\n",
       " 'timestamp': 1586715323,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': '06841e7fb1a3ca89e4e1e9725e493846',\n",
       "    'location': {'left': 447.42,\n",
       "     'top': 430.05,\n",
       "     'width': 263,\n",
       "     'height': 222,\n",
       "     'rotation': 0},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -23.43, 'pitch': 21.28, 'roll': 6.66},\n",
       "    'age': 6,\n",
       "    'gender': {'type': 'male', 'probability': 0.95},\n",
       "    'expression': {'type': 'smile', 'probability': 1},\n",
       "    'emotion': {'type': 'happy', 'probability': 0.99}}]}}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://aip.bdstatic.com/portal-pc-node/dist/1586441726384/images/technology/face/detect/demo-card-2.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"age,gender,expression,emotion\\\"}\"\n",
    "\n",
    "access_token = \"24.fec551752e09dd64cb0861066cc729d2.2592000.1589305576.282335-19396885\"\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "r = requests.post(request_url, data=params, headers=headers)\n",
    "r.status_code\n",
    "print(r.content)\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 图2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"error_code\":0,\"error_msg\":\"SUCCESS\",\"log_id\":4545897584758,\"timestamp\":1586715658,\"cached\":0,\"result\":{\"face_num\":1,\"face_list\":[{\"face_token\":\"00b4777d33deacd88f103ccfdd3f0b04\",\"location\":{\"left\":136.68,\"top\":126.76,\"width\":89,\"height\":86,\"rotation\":0},\"face_probability\":1,\"angle\":{\"yaw\":-10.27,\"pitch\":6.65,\"roll\":-1.76},\"age\":23,\"gender\":{\"type\":\"male\",\"probability\":0.99},\"expression\":{\"type\":\"smile\",\"probability\":0.96},\"emotion\":{\"type\":\"happy\",\"probability\":1}}]}}'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 4545897584758,\n",
       " 'timestamp': 1586715658,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': '00b4777d33deacd88f103ccfdd3f0b04',\n",
       "    'location': {'left': 136.68,\n",
       "     'top': 126.76,\n",
       "     'width': 89,\n",
       "     'height': 86,\n",
       "     'rotation': 0},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -10.27, 'pitch': 6.65, 'roll': -1.76},\n",
       "    'age': 23,\n",
       "    'gender': {'type': 'male', 'probability': 0.99},\n",
       "    'expression': {'type': 'smile', 'probability': 0.96},\n",
       "    'emotion': {'type': 'happy', 'probability': 1}}]}}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"http://newmedia.nfu.edu.cn/wcy/wp-content/uploads/2018/04/post_20180424__NFU_DoraHacks_imoji%E5%9B%A2%E9%98%9F.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"age,gender,expression,emotion\\\"}\"\n",
    "\n",
    "access_token = \"24.fec551752e09dd64cb0861066cc729d2.2592000.1589305576.282335-19396885\"\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "r = requests.post(request_url, data=params, headers=headers)\n",
    "r.status_code\n",
    "print(r.content)\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 图3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"error_code\":0,\"error_msg\":\"SUCCESS\",\"log_id\":8405794505457,\"timestamp\":1586715799,\"cached\":0,\"result\":{\"face_num\":1,\"face_list\":[{\"face_token\":\"e9f56439644d4380156bfcd27421805f\",\"location\":{\"left\":184.73,\"top\":80.7,\"width\":83,\"height\":90,\"rotation\":4},\"face_probability\":1,\"angle\":{\"yaw\":-24.89,\"pitch\":7.77,\"roll\":6.51},\"age\":22,\"gender\":{\"type\":\"female\",\"probability\":1},\"expression\":{\"type\":\"smile\",\"probability\":0.99},\"emotion\":{\"type\":\"happy\",\"probability\":0.99}}]}}'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 8405794505457,\n",
       " 'timestamp': 1586715799,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': 'e9f56439644d4380156bfcd27421805f',\n",
       "    'location': {'left': 184.73,\n",
       "     'top': 80.7,\n",
       "     'width': 83,\n",
       "     'height': 90,\n",
       "     'rotation': 4},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -24.89, 'pitch': 7.77, 'roll': 6.51},\n",
       "    'age': 22,\n",
       "    'gender': {'type': 'female', 'probability': 1},\n",
       "    'expression': {'type': 'smile', 'probability': 0.99},\n",
       "    'emotion': {'type': 'happy', 'probability': 0.99}}]}}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2143251794,186087008&fm=26&gp=0.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"age,gender,expression,emotion\\\"}\"\n",
    "\n",
    "access_token = \"24.fec551752e09dd64cb0861066cc729d2.2592000.1589305576.282335-19396885\"\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "r = requests.post(request_url, data=params, headers=headers)\n",
    "r.status_code\n",
    "print(r.content)\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 结论：\n",
    "\n",
    "1、重要的信息：\n",
    "* Azure：faceID、KEY、BASE_URL；\n",
    "* Face++：request_id、api_secret、api_key、BASE_URL；\n",
    "* 百度：face_token、access_token（通过ASKS获得）、request_url；\n",
    "\n",
    "2、将混乱的json变成易看的json\n",
    "\n",
    "\n",
    "r = requests.post(request_url/BASE_URL, data=params, headers=headers)\n",
    "r.status_code\n",
    "print(r.content)\n",
    "results = r.json()\n",
    "results\n",
    "\n",
    "\n",
    "3、百度的人脸检测所谓的“AK、SK”:\n",
    "* 为**应用列表**的AKSK（控制台-->应用列表-->API Key、Secret Key）\n",
    "* 而非账户本身的AKSK（控制台-->安全认证-->Access Key、Secret Key）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
